package com.test.netty4server.discard;

 

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

public class DiscardServer {
	private int port ;

	public DiscardServer(int port) {
		super();
		this.port = port;
	}
	
	
	public static void main(String[] args) {
		DiscardServer server = new DiscardServer(9000);
		server.run();
	}
	
	public void run(){
		EventLoopGroup  bossGroup = new  NioEventLoopGroup(1);
		EventLoopGroup workerGroup = new  NioEventLoopGroup();
		ServerBootstrap  b = new ServerBootstrap();
		b.group(bossGroup, workerGroup)
		.channel(NioServerSocketChannel.class)
		.handler(new LoggingHandler(LogLevel.INFO))
		.childHandler( new ChannelInitializer<SocketChannel>() {
			@Override
			protected void initChannel(SocketChannel ch) throws Exception {
				 ch.pipeline().addLast(new DiscardServerHandler());
				
			}
		})
		.option(ChannelOption.SO_BACKLOG, 128)
		.option(ChannelOption.SO_KEEPALIVE, true);
		
		try {
			// Bind and start to accept incoming connections.
			ChannelFuture f = b.bind(port).sync();
			
			// shut down your server.
			f.channel().closeFuture().sync();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			workerGroup.shutdownGracefully() ; 
			bossGroup.shutdownGracefully() ; 
		}
			
	}
}
